package work.base.designpattern.strategy;

// 策略接口：折扣计算
interface DiscountStrategy {
    double calculate(double price);
}

// 具体策略：普通会员折扣
class NormalMemberDiscount implements DiscountStrategy {
    @Override
    public double calculate(double price) {
        System.out.println("普通会员，没有折扣");
        return price;
    }
}

// 具体策略：黄金会员折扣
class GoldMemberDiscount implements DiscountStrategy {
    @Override
    public double calculate(double price) {
        System.out.println("黄金会员，打9折");
        return price * 0.9;
    }
}

// 具体策略：白金会员折扣
class PlatinumMemberDiscount implements DiscountStrategy {
    @Override
    public double calculate(double price) {
        System.out.println("白金会员，打8折");
        return price * 0.8;
    }
}

// 上下文：订单
class Order {
    private DiscountStrategy discountStrategy;

    public Order(DiscountStrategy discountStrategy) {
        this.discountStrategy = discountStrategy;
    }

    public void setDiscountStrategy(DiscountStrategy discountStrategy) {
        this.discountStrategy = discountStrategy;
    }

    public double calculatePrice(double price) {
        return discountStrategy.calculate(price);
    }
}

// 测试类
public class StrategyPatternDemo {
    public static void main(String[] args) {
        // 创建不同会员的折扣策略
        DiscountStrategy normalStrategy = new NormalMemberDiscount();
        DiscountStrategy goldStrategy = new GoldMemberDiscount();
        DiscountStrategy platinumStrategy = new PlatinumMemberDiscount();

        // 创建订单并应用不同的折扣策略
        double price = 1000.0;
        System.out.println("原价: " + price);

        // 普通会员价格
        Order order = new Order(normalStrategy);
        double normalPrice = order.calculatePrice(price);
        System.out.println("普通会员价格: " + normalPrice);

        // 切换为黄金会员策略
        order.setDiscountStrategy(goldStrategy);
        double goldPrice = order.calculatePrice(price);
        System.out.println("黄金会员价格: " + goldPrice);

        // 切换为白金会员策略
        order.setDiscountStrategy(platinumStrategy);
        double platinumPrice = order.calculatePrice(price);
        System.out.println("白金会员价格: " + platinumPrice);
    }
}
